


import bpy
import os

import 物体.物体管理.物体数据 as 网格数据

from 节点.全局控制.Node.数据编辑.物体 import bl_修改器公共选项

from 节点.全局控制.Node.节点和插座基类 import bl_节点基类
from 节点.Core import bl_UI配色
from 节点.Core import bl_Mesh

import engine.initLIB.load_dll as engineLib
import ctypes


engineLib.flexCAPI.f_创建砖块地形纹理置换节点.restype = ctypes.c_bool



def f_加载地形纹理(self, mat, name, 检查已有纹理, x, y):
	i = mat.material.node_tree.nodes.find(name)
	
	if i >= 0 :
		node = mat.material.node_tree.nodes[i]
		path = self.m_路径 + '/' + name + '_x' + str(x) + '_y'+str(y) + '.png'
		if node.image and node.image.filepath == path:
			node.image.reload()
		elif os.path.exists(path):
			node.image = bpy.data.images.load(path, check_existing=检查已有纹理)

	i = mat.material.node_tree.nodes.find(name + '_normal')
	if i >= 0 :
		node = mat.material.node_tree.nodes[i]
		path = self.m_路径 + '/' + name + '_normal' + '_x' + str(x) + '_y'+str(y) + '.png'
		
		if node.image and node.image.filepath == path:
			node.image.reload()
		elif os.path.exists(path):
			node.image = bpy.data.images.load(path, check_existing=检查已有纹理)
			#print('f_加载地形纹理 = ', path, os.path.exists(path))


class C_Node_砖块地形纹理置换(bl_节点基类.C_Node_基类):
	bl_dname = 'bl_node_砖块地形纹理置换'
	bl_label = '砖块地形纹理置换'
	
	m_维度 = bpy.props.IntVectorProperty(size=2, default=(1, 1))
	m_路径 = bpy.props.StringProperty(subtype='DIR_PATH')
	m_重载 = bpy.props.BoolProperty(default=False)

	m_高度名称 = bpy.props.StringProperty(default='height')
	m_Flow名称 = bpy.props.StringProperty(default='flow')
	m_侵蚀名称 = bpy.props.StringProperty(default='wear')
	m_沉积名称 = bpy.props.StringProperty(default='despoition')

	m_材质名称 = bpy.props.StringProperty(default='地形材质')

	m_位置补偿 = bpy.props.FloatVectorProperty(size=2, default=(1,1))
	#m_纹理节点 = bpy.props.StringProperty(default='Image Texture')

	m_修改器名称 = bpy.props.StringProperty(default='地形置换')
	m_是否渲染 = bpy.props.BoolProperty(default=True, update=bl_修改器公共选项.f_update_修改器可渲)
	m_是否显示 = bpy.props.BoolProperty(default=True, update=bl_修改器公共选项.f_update_修改器可视)

	def init(self, context):
		super(C_Node_砖块地形纹理置换, self).f_init(context)
		self.inputs.new('C_Socket_物体_Array1D', name='物体')
		self.inputs.new('C_Socket_F32', name='高度')
		#self.outputs.new('C_Socket_网格_Array1D', name='网格')
		
		self.f_toCPP();

	def draw_buttons(self, context, layout):
		bl_修改器公共选项.f_可视选项绘制(self, layout)

		layout.separator()
		layout.prop(self, 'm_位置补偿')

		layout.separator()
		layout.prop(self, 'm_维度')
		layout.prop(self, 'm_路径')
		layout.prop(self, 'm_重载')
		layout.separator()
		layout.prop(self, 'm_高度名称')
		layout.prop(self, 'm_Flow名称')
		layout.prop(self, 'm_侵蚀名称')
		layout.prop(self, 'm_沉积名称')
		layout.separator()
		layout.prop(self, 'm_材质名称')
		layout.prop(self, 'm_修改器名称')

		
	def f_toCPP(self):
		是否更新 = engineLib.flexCAPI.f_创建砖块地形纹理置换节点(ctypes.c_wchar_p(self.id_data.name), ctypes.c_wchar_p(self.name), self.f_getPyObject());
		if 是否更新 :
			self.inputs['高度'].f_update()
			self.use_custom_color = True
			self.color = bl_UI配色.g_物体类节点颜色

	def f_update(self):
		if engineLib.flexCAPI.f_node_是否即时更新节点() == False : return

		xDim = self.m_维度[0]
		yDim = self.m_维度[1]

		物体 = self.inputs['物体'].f_getData()
		if len(物体) < xDim*yDim : return

		高度 = self.inputs['高度'].f_getData()

		offset = 0
		for y in range(yDim):
			for x in range(xDim):
				obj = 物体[offset]
				offset += 1

				#print('m_位置补偿 = ', self.m_位置补偿[0], self.m_位置补偿[1])
				obj.location.x = obj.dimensions.x * (x - int(xDim/2)) * self.m_位置补偿[0]
				obj.location.y = obj.dimensions.y * (y - int(yDim/2)) * self.m_位置补偿[1]
				#obj.z = obj.dimensions.y * z



				修改器ID = obj.modifiers.find("地形置换")
				if 修改器ID >= 0 :
					修改器 = obj.modifiers[修改器ID]
				else:
					修改器 = obj.modifiers.new('地形置换', 'DISPLACE')
				
				检查已有纹理 = self.m_重载
				if 检查已有纹理 : 检查已有纹理 = False
				else : 检查已有纹理 = True

				
				if 修改器.texture == None :
					修改器.texture = bpy.data.textures.new(obj.name+'地形置换', 'IMAGE')
				if 修改器.texture.image == None :
					path = self.m_路径 + '/' + self.m_高度名称 + '_x' + str(x) + '_y'+str(y) + '.png'
					修改器.texture.image = bpy.data.images.load(path, check_existing=检查已有纹理)
				elif self.m_重载 :
					修改器.texture.image.reload()

				修改器.texture.extension = 'EXTEND'
				修改器.strength = 高度

				if len(obj.material_slots) :
					mat = obj.material_slots[0]
					#mat = obj.material_slots[i]
					#print('m_材质名称 = ', mat)
					if mat and mat.material:
						f_加载地形纹理(self, mat, self.m_高度名称, 检查已有纹理, x, y)
						f_加载地形纹理(self, mat, self.m_Flow名称, 检查已有纹理, x, y)
						f_加载地形纹理(self, mat, self.m_侵蚀名称, 检查已有纹理, x, y)
						f_加载地形纹理(self, mat, self.m_沉积名称, 检查已有纹理, x, y)





class C_NODE_MENU_砖块地形纹理置换(bpy.types.Operator):
	bl_idname = 'node.global_node_terrain_displace_modifiers'
	bl_label = '砖块地形纹理置换'
	bl_options = {'REGISTER', 'UNDO'}
	def execute(operator, context):
		bpy.ops.node.add_node(type='C_Node_砖块地形纹理置换', use_transform=True)
		return {'FINISHED'}







classes = (
	C_Node_砖块地形纹理置换,
	C_NODE_MENU_砖块地形纹理置换,


)

def register():
	for c in classes:
		bpy.utils.register_class(c)
		
def unregister():
	for c in classes:
		bpy.utils.unregister_class(c)







